/*****************************************************************
                        Input Frame
PURPOSE: This is the input view that prompts user for input in order to 
run simulation. This view is meant for the instructor, author.
*****************************************************************/
import java.awt.*;

public class inputFrame extends Frame {
   /**
	 * 
	 */
	private static final long serialVersionUID = 7347084217936982909L;
TextArea proc,arriv,serv;
   Panel knobs;
   Choice alg;
   CheckboxGroup functions;
   Checkbox[] fun;
   Packet info;
   GUI vigil;
   String PrevBut;

   /*--------------------------------------------------------
                        Constructor
   PURPOSE:  To generate the input view frame, and to ask animation view to be ready
   PARAMETERS: references the animation view
   --------------------------------------------------------*/
   public inputFrame(AlgAnime parent) {
      super("Input View");
      vigil = new GUI();
      vigil.addObserver((java.util.Observer)parent);

      String sampleP = "1\n2\n3\n4\n5";
      String sampleA = "0\n2\n4\n6\n8";
      String sampleS = "3\n6\n4\n5\n2";
      
      proc = new TextArea(30,10);
      proc.setEditable(true);
      proc.appendText(sampleP);

      arriv = new TextArea(30,5);
      arriv.setEditable(true);
      arriv.appendText(sampleA);
      
      serv = new TextArea(30,5);
      serv.setEditable(true);
      serv.appendText(sampleS);

      alg = new Choice();
      alg.addItem("FCFS");
      alg.addItem("RR1");
      alg.addItem("RR4");
      alg.addItem("SPN");
      alg.addItem("SRT");
      alg.addItem("HRRN");
      alg.addItem("FB1");

      functions = new CheckboxGroup();
      fun = new Checkbox[5];
      fun[0] = new Checkbox("clear",functions,false);
      fun[1] = new Checkbox("run",functions,false);
      fun[2] = new Checkbox("pause",functions,false);
      fun[3] = new Checkbox("resume",functions,false);
      fun[4] = new Checkbox("quit",functions,false);
      PrevBut = ""; // init

      knobs = new Panel();
      knobs.setLayout(new FlowLayout(FlowLayout.CENTER));
      knobs.add(alg);
      knobs.add(fun[0]);
      knobs.add(fun[1]);
      knobs.add(fun[2]);
      knobs.add(fun[3]);
      knobs.add(fun[4]);

      Panel labels = new Panel();
      labels.setLayout(new BorderLayout());
      labels.add("West", new Label("Arrival time:"));     
      labels.add("Center", new Label("Process name:"));
      labels.add("East", new Label("Service time:")); 

      this.setLayout(new BorderLayout());
      this.add("North",labels);
      this.add("West", arriv);
      this.add("Center",proc);
      this.add("East",serv);
      this.add("South", knobs);      
   } // set display

   /*--------------------------------------------------------
                    Handle Event
   PURPOSE:  To handle all events by dialog box
   --------------------------------------------------------*/
   public boolean handleEvent (Event evtObj) {
      if (evtObj.id == Event.WINDOW_DESTROY) {
         this.dispose();
         return true;
      } // destroy button
      else if (evtObj.id==Event.ACTION_EVENT)
         if (evtObj.target==fun[0]) {
            proc.setText("");
            arriv.setText("");
            serv.setText("");
            fun[1].enable();
            String cmd = functions.getCurrent().getLabel();
            vigil.input(cmd);
            return true;
         } // handle clear button
         else if (evtObj.target==fun[1]) {
            fun[1].disable();
            info = new Packet(proc.getText(), arriv.getText(), serv.getText(), alg.getSelectedItem());
            vigil.input(info);
            proc.setEditable(false);
            arriv.setEditable(false);
            serv.setEditable(false);
            return true;
         } // handle run button
         else if (evtObj.target==fun[2] || evtObj.target==fun[3]) {
            String cmd = functions.getCurrent().getLabel();
            if (PrevBut.equals(cmd))
               return false;
            else {
               PrevBut = cmd; // stagger for next event
               vigil.input(cmd);
               return true;
            } // balance pause to resume request
         } // handle pause/resume buttons
         else if (evtObj.target==fun[4]) {
            vigil.input("quit");
            return true;
         } // handle quit option
      return false;      
   } // handle event

   /*--------------------------------------------------------
                    Reset input GUI
   PURPOSE:  To enable more input
   --------------------------------------------------------*/
   public void resetGUI() {
      proc.setEditable(true);
      serv.setEditable(true);
      arriv.setEditable(true);
      fun[1].enable();
   } // allow user input
} // input Frame
